package multiplex.view;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;

import javax.swing.AbstractListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListModel;

import multiplex.facade.Facade;
import multiplex.model.Filme;



public class IFTelaGerenciaDestaqueMarketing extends InternalFramePrincipal {
	

	  public Facade meuFacade; 
		
	  private JList sourceList;

	  private SortedListModel sourceListModel;

	  private JList destList;

	  private SortedListModel destListModel;

	  private JButton addButton;

	  private JButton removeButton;

	  private JButton salvarButton;
	  
	  private JButton fecharButton;
	  
	  private Container conteudo;
	  
	  public IFTelaGerenciaDestaqueMarketing(){
			super("Gerenciar Destaques De Marketing");
					
			this.conteudo = getContentPane();   
			conteudo.setLayout(new BorderLayout());
		
		try{  	    
	        this.meuFacade = new Facade();
	       }catch(Exception e){
			  JOptionPane.showMessageDialog(this.conteudo, e.toString());
			  this.sourceListModel = new SortedListModel();
			  System.out.println("Source list MOdel");
		      this.destListModel = new SortedListModel();
		      System.out.println("dest list MOdel");
			    initScreen();
		  }
	  }
		

	  public void clearSourceListModel() {
	    sourceListModel.clear();
	  }

	  public void clearDestinationListModel() {
	    destListModel.clear();
	  }

	  public void addSourceElements(ListModel newValue) {
	    fillListModel(sourceListModel, newValue);
	  }

	  public void setSourceElements(ListModel newValue) {
	    clearSourceListModel();
	    addSourceElements(newValue);
	  }

	  public void addDestinationElements(ListModel newValue) {
	    fillListModel(destListModel, newValue);
	  }

	  private void fillListModel(SortedListModel model, ListModel newValues) {
	    int size = newValues.getSize();
	    for (int i = 0; i < size; i++) {
	      model.add(newValues.getElementAt(i));
	    }
	  }

	  public void addSourceElements(Object newValue[]) {
	    fillListModel(sourceListModel, newValue);
	  }

	  public void setSourceElements(Object newValue[]) {
	    clearSourceListModel();
	    addSourceElements(newValue);
	  }

	  public void addDestinationElements(Object newValue[]) {
	    fillListModel(destListModel, newValue);
	  }

	  private void fillListModel(SortedListModel model, Object newValues[]) {
	    model.addAll(newValues);
	  }

	  private void clearSourceSelected() {
	    Object selected[] = sourceList.getSelectedValues();
	    for (int i = selected.length - 1; i >= 0; --i) {
	      sourceListModel.removeElement(selected[i]);
	    }
	    sourceList.getSelectionModel().clearSelection();
	  }

	  private void clearDestinationSelected() {
	    Object selected[] = destList.getSelectedValues();
	    for (int i = selected.length - 1; i >= 0; --i) {
	      destListModel.removeElement(selected[i]);
	    }
	    destList.getSelectionModel().clearSelection();
	  }

	  private void initScreen() {
	        
	    fillDestinationList();
	    fillSourceList();
	    
	    sourceList = new JList(sourceListModel);
	    destList = new JList(destListModel);
	    
	    addButton = new JButton("Destaques >>");
	    addButton.addActionListener((ActionListener) new AddListener());
	    
	    removeButton = new JButton("<< Não Destaques");
	    removeButton.addActionListener(new RemoveListener());

	    JPanel centerPanel = new JPanel(new GridLayout(2,1));
	    centerPanel.add(addButton);
	    centerPanel.add(removeButton);
	    

	    salvarButton = new JButton("Salvar");
	    salvarButton.addActionListener(new SalvarListener());
	    
	    fecharButton = new JButton("Fechar");
	    fecharButton.addActionListener(new CancelarListener());
	    
	    JPanel southPanel = new JPanel(new GridLayout(1,2));
	    southPanel.add(salvarButton);
	    southPanel.add(fecharButton);
	    
	    //filmes não destaque
	    JPanel leftPanel = new JPanel(new BorderLayout());
	    leftPanel.add(new JLabel("Filmes Disponíveis"), BorderLayout.NORTH);
	    leftPanel.add(new JScrollPane(sourceList), BorderLayout.CENTER);


	    //filmes destaque
	    JPanel rightPanel = new JPanel(new BorderLayout());
	    rightPanel.add(new JLabel("Filmes Destaques"), BorderLayout.NORTH);
	    rightPanel.add(new JScrollPane(destList), BorderLayout.CENTER);
    
	    this.conteudo.add(leftPanel,BorderLayout.WEST);
	    this.conteudo.add(rightPanel,BorderLayout.EAST);
	    this.conteudo.add(centerPanel,BorderLayout.CENTER);
	    this.conteudo.add(southPanel,BorderLayout.SOUTH);
	    
	  }

	  private class AddListener implements ActionListener {
	    public void actionPerformed(ActionEvent e) {
	      Object selected[] = sourceList.getSelectedValues();
	      addDestinationElements(selected);
	      clearSourceSelected();
	    }


	  }

	  private class RemoveListener implements ActionListener {
	    public void actionPerformed(ActionEvent e) {
	      Object selected[] = destList.getSelectedValues();
	      addSourceElements(selected);
	      clearDestinationSelected();
	    }
	    
	  }

	  private class SalvarListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			
		}
		  
	  }
	  
	  private class CancelarListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			
		}
		  
	  }

	  public void fillSourceList(){
	  try{
		List<Filme> aLista = this.meuFacade.getNaoDestaques();
		this.sourceListModel.addAll(aLista.toArray());
	  }
	  catch (Exception e) {
		// TODO: handle exception
		  JOptionPane.showMessageDialog(this.conteudo, e.toString());	}
	  }//preenche combo de filmes
	  
	  public void fillDestinationList(){
	  try{
		List<Filme> aLista = this.meuFacade.getDestaques();
		this.destListModel.addAll(aLista.toArray());
	  }
	  catch (Exception e){
		  JOptionPane.showMessageDialog(this.conteudo, e.toString());	  }
	  }//preenche com destaques
	  
	
	
	private class SortedListModel extends AbstractListModel{
		private SortedSet<Object> model;
	
		public SortedListModel(){
		model = new TreeSet<Object>();
	}
	
	@Override
		public Object getElementAt(int arg0) {
		// TODO Auto-generated method stub
		return model.toArray()[arg0];
	}

	@Override
		public int getSize() {
		// TODO Auto-generated method stub
		return model.size();
	}
	
	 	public void add(Object element) {
		    if (model.add(element)) {
		      fireContentsChanged(this, 0, getSize());
		    }
		  }

		  public void addAll(Object elements[]) {
		    Collection<Object> c = Arrays.asList(elements);
		    model.addAll(c);
		    fireContentsChanged(this, 0, getSize());
		  }

		  public void clear() {
		    model.clear();
		    fireContentsChanged(this, 0, getSize());
		  }

		  public boolean contains(Object element) {
		    return model.contains(element);
		  }

		  public Object firstElement() {
		    return model.first();
		  }

		  public Iterator<Object> iterator() {
		    return model.iterator();
		  }

		  public Object lastElement() {
		    return model.last();
		  }

		  public boolean removeElement(Object element) {
		    boolean removed = model.remove(element);
		    if (removed) {
		      fireContentsChanged(this, 0, getSize());
		    }
		    return removed;
		  }
}



	public static void main(String args[]){
		
		JFrame aTela = new JFrame();
		aTela.setSize(new Dimension(800,600));
		
		IFTelaGerenciaDestaqueMarketing aTelaMarketing = new IFTelaGerenciaDestaqueMarketing();
		
		aTela.add(aTelaMarketing);
		aTela.setVisible(true);
		
		aTelaMarketing.setVisible(true);
		
	}
  }